﻿using LoveEatRabbit.API.Write.Applications.Commands.Permissions;
using LoveEatRabbit.Domain.RBAC;
using LoveEatRabbit.ErrorCode;
using LoveEatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace LoveEatRabbit.API.Write.Applications.CommandHandlers.Permissions
{
    public class PermissionCreatedCommandHandler : IRequestHandler<PermissionCreatedCommand, APIResult<int>>
    {
        private readonly IBaseRespository<PermissionModel> PerRep;

        public PermissionCreatedCommandHandler(IBaseRespository<PermissionModel> perRep)
        {
            PerRep = perRep;
        }

        public async Task<APIResult<int>> Handle(PermissionCreatedCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code = APIResultCode.添加成功;
            result.Message = APIResultCode.添加成功.ToString();

            PermissionModel per = new PermissionModel();
            per.PermissionURL = request.PermissionURL;
            per.PermissionName = request.PermissionName;
            per.Index = request.Index;
            await PerRep.AddAsync(per);

            return await Task.FromResult(result);
        }
    }
}
